bitset: Fix the right-shift implementation
authorMatthias Clasen <mclasen@redhat.com>
Sun, 28 Jun 2020 20:36:03 +0000 (16:36 -0400)
committerMatthias Clasen <mclasen@redhat.com>
Sun, 28 Jun 2020 20:37:30 +0000 (16:37 -0400)
This was not doing the right thing at all.

This commit also adds tests for left- and
right-shift.

gtk/gtkbitset.c
testsuite/gtk/bitset.c

index 944b899de60bbc627ba50fc28d523ec45d6717f0..c4116bd87cba33e5f42fb8a15db80ec8c1de5571 100644 (file)
@@ -604,8 +604,8 @@ gtk_bitset_shift_right (GtkBitset *self,
   original = gtk_bitset_copy (self);
   gtk_bitset_remove_all (self);
 
-  for (loop = gtk_bitset_iter_init_at (&iter, original, amount, &value);
-       loop && value >= G_MAXUINT - amount;
+  for (loop = gtk_bitset_iter_init_first (&iter, original, &value);
+       loop && value <= G_MAXUINT - amount;
        loop = gtk_bitset_iter_next (&iter, &value))
     {
       gtk_bitset_add (self, value + amount);
index 5c7d0d16d97756340893a3c660bc98e1b56170ad..de7d4654eb8612d50db7365fd3db104ae58d56e7 100644 (file)
@@ -350,6 +350,74 @@ test_subtract (void)
     }
 }
 
+static void
+test_shift_left (void)
+{
+  guint i, j, k, min, max;
+  GtkBitset *iset, *testset;
+
+  for (i = 0; i < G_N_ELEMENTS (bitsets); i++)
+    {
+      iset = bitsets[i].create();
+
+      for (j = 1; j < 10000000; j *= 10)
+        {
+          testset = gtk_bitset_copy (iset);
+
+          gtk_bitset_shift_left (testset, j);
+
+          min = MIN (gtk_bitset_get_minimum (iset), gtk_bitset_get_minimum (testset));
+          max = MAX (gtk_bitset_get_maximum (iset), gtk_bitset_get_maximum (testset));
+
+          for (k = min; k <= max; k++)
+            {
+              if (k >= j)
+                g_assert_cmpint (gtk_bitset_contains (iset, k), ==, gtk_bitset_contains (testset, k - j));
+            }
+
+          gtk_bitset_unref (testset);
+        }
+
+       gtk_bitset_unref (iset);
+    }
+}
+
+static void
+test_shift_right (void)
+{
+  guint i, j, k, min, max;
+  GtkBitset *iset, *testset;
+
+  for (i = 0; i < G_N_ELEMENTS (bitsets); i++)
+    {
+      iset = bitsets[i].create();
+
+      for (j = 1; j < 10000000; j *= 10)
+        {
+          testset = gtk_bitset_copy (iset);
+
+          gtk_bitset_shift_right (testset, j);
+
+          min = MIN (gtk_bitset_get_minimum (iset), gtk_bitset_get_minimum (testset));
+          max = MAX (gtk_bitset_get_maximum (iset), gtk_bitset_get_maximum (testset));
+
+          for (k = min; k <= max; k++)
+            {
+              if (k <= G_MAXUINT - j)
+                {
+                if (gtk_bitset_contains (iset, k) != gtk_bitset_contains (testset, k + j))
+                  g_print ("right-shift fail set %u shift %u test %u\n", i, j, k);
+                g_assert_cmpint (gtk_bitset_contains (iset, k), ==, gtk_bitset_contains (testset, k + j));
+                }
+            }
+
+          gtk_bitset_unref (testset);
+        }
+
+       gtk_bitset_unref (iset);
+    }
+}
+
 int
 main (int argc, char *argv[])
 {
@@ -364,6 +432,8 @@ main (int argc, char *argv[])
   g_test_add_func ("/bitset/intersect", test_intersect);
   g_test_add_func ("/bitset/difference", test_difference);
   g_test_add_func ("/bitset/subtract", test_subtract);
+  g_test_add_func ("/bitset/shift-left", test_shift_left);
+  g_test_add_func ("/bitset/shift-right", test_shift_right);
 
   return g_test_run ();
 }